class code5 {
    int m;
    int n;
    public int maxAreaOfIsland(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int ret = 0;

        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                if(grid[i][j]==1) {
                    ret = Math.max(ret,dfs(grid,i,j));
                }
            }
        }
        return ret;
    }

    public int dfs(int[][] grid,int i,int j) {
        if(board(i,j)) {
            return 0;
        }

        if(grid[i][j] != 1) {
            return 0;
        }
        //做标记，遍历过的
        grid[i][j] = 2;

        return 1+dfs(grid,i+1,j)+dfs(grid,i-1,j)+dfs(grid,i,j-1)+dfs(grid,i,j+1);
    }

    public boolean board(int i,int j) {
        if(i<0||i>=m||j<0||j>=n) {
            return true;
        }
        return false;
    }
}